package com.dongdongshop.servcice.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.*;
import com.dongdongshop.pojo.*;
import com.dongdongshop.pojo.vo.DetailsVo;
import com.dongdongshop.pojo.vo.GoodsVo;
import com.dongdongshop.service.GoodsService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

@Service
@Component
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private TbItemCatMapper itemCatMapper;
    @Resource
    private TbGoodsMapper goodsMapper;
    @Resource
    private TbGoodsDescMapper goodsDescMapper;
    @Resource
    private TbBrandMapper brandMapper;
    @Resource
    private TbSellerMapper sellerMapper;
    @Resource
    private TbItemMapper itemMapper;
    //mq
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public List<TbItemCat> getGoodInfo(Integer pid) {
        TbItemCatExample example =new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(Long.valueOf(pid));
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(example);
        return tbItemCats;
    }

    @Transactional //声明事务
    @Override
    public void insertGoods(TbGoods goods, TbGoodsDesc goodsDesc,List<TbItem> list) {
        //增加并返回id
        goodsMapper.insertGoodsAndBackId(goods);
        //商品详细表
        goodsDesc.setGoodsId(goods.getId());
        goodsDescMapper.insertSelective(goodsDesc);

        Stream<TbItem> stream = list.stream();
        Consumer<TbItem> consumer = x ->{
            //商品标题
            x.setTitle(goods.getGoodsName());
            //商品卖点(商品表里的副标题)
            x.setSellPoint(goods.getCaption());
            //所属类目(商品表里的3级id)
            x.setCategoryid(goods.getCategory3Id());
            //'商品状态
            x.setStatus("1");
            //创建时间
            x.setCreateTime(new Date());
            //更新时间
            x.setUpdateTime(new Date());
            //商品id
            x.setGoodsId(goods.getId());
            //商家id
            x.setSellerId(goods.getSellerId());
            //商品类目
            TbItemCat itemCat = itemCatMapper.selectByPrimaryKey(goods.getCategory3Id());
            x.setCategory(itemCat.getName());
            //品牌
            TbBrand brand = brandMapper.selectByPrimaryKey(goods.getBrandId());
            x.setBrand(brand.getName());
            //商家名称
            TbSeller seller = sellerMapper.selectByPrimaryKey(goods.getSellerId());
            x.setSeller(seller.getName());
            itemMapper.insertSelective(x);
        };
        stream.forEach(consumer);


        /*for (TbItem item1 :list){
            //商品标题
            item1.setTitle(goods.getGoodsName());
            //商品卖点(商品表里的副标题)
            item1.setSellPoint(goods.getCaption());
            //所属类目(商品表里的3级id)
            item1.setCategoryid(goods.getCategory3Id());
            //'商品状态
            item1.setStatus("1");
            //创建时间
            item1.setCreateTime(new Date());
            //更新时间
            item1.setUpdateTime(new Date());
            //商品id
            item1.setGoodsId(goods.getId());
            //商家id
            item1.setSellerId(goods.getSellerId());
            //商品类目
            TbItemCat itemCat = itemCatMapper.selectByPrimaryKey(goods.getCategory3Id());
            item1.setCategory(itemCat.getName());
            //品牌
            TbBrand brand = brandMapper.selectByPrimaryKey(goods.getBrandId());
            item1.setBrand(brand.getName());
            //商家名称
            TbSeller seller = sellerMapper.selectByPrimaryKey(goods.getSellerId());
            item1.setSeller(seller.getName());
            itemMapper.insertSelective(item1);
        }*/
    }

    @Override
    public List<GoodsVo> getGoodsInfo(String sellerId,Integer auditStatus,String goodsName) {
        List<GoodsVo> tbGoods = goodsMapper.selectByGoodsVo(sellerId,auditStatus,goodsName);
        return tbGoods;
    }

    //删除
    @Override
    public void deleteBatch(Long[] ids) {
        Stream<Long> ids1 = Stream.of(ids);
        Consumer<Long> consumer = x ->{
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(x);
            tbGoods.setIsDelete("2");
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        };
        ids1.forEach(consumer);
        /*for (int i = 0;i <ids.length ;i++){
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(ids[i]);
            tbGoods.setIsDelete("2");
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        }*/
    }
    //提交审核
    @Override
    public void SubmitReview(Long[] ids,String statu) {
        if (statu.equals("2")){
            for (Long id : ids) {
                rocketMQTemplate.convertAndSend("cctv5",id);
            }
        }

        Stream<Long> ids1 = Stream.of(ids);
        Consumer<Long> consumer = x ->{
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(x);
            tbGoods.setAuditStatus(statu);
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        };
        ids1.forEach(consumer);

        /*for (int i = 0;i <ids.length ;i++){
            TbGoods tbGoods = goodsMapper.selectByPrimaryKey(ids[i]);
            tbGoods.setAuditStatus(statu);
            goodsMapper.updateByPrimaryKeySelective(tbGoods);
        }*/
    }

    @Override
    public List<GoodsVo> getGoodsShInfo(String goodsName) {
        List<GoodsVo> goodsVos = goodsMapper.selectByGoodsShVo(goodsName);
        return goodsVos;
    }

    @Override
    public List<DetailsVo> getDetailsById(Long id) {
        List<DetailsVo> detailsVos = goodsMapper.getDetailsById(id);
        return detailsVos;
    }

    @Override
    public TbGoods findGoodsById(Long id) {
        return goodsMapper.selectByPrimaryKey(id);
    }
}
